package com.yunzhong.appointment.illnesstpye.controller;

import com.github.pagehelper.PageInfo;
import com.yunzhong.appointment.entity.Illness;
import com.yunzhong.appointment.entity.Illnessposition;
import com.yunzhong.appointment.illnesstpye.entity.ResultJson;
import com.yunzhong.appointment.illnesstpye.service.IllnessService;
import com.yunzhong.appointment.illnesstpye.service.IllnessTypeService;
import com.yunzhong.appointment.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Controller
public class IllnessController {

    @Autowired
    private IllnessService illnessService;
    @Autowired
    private IllnessTypeService illnessTypeService;
    @RequestMapping("/illness/query")
    public String query(ModelMap modelMap, HttpServletRequest request) {
        PageData pd = new PageData(request);
        List<Illness> list = illnessService.query(pd);
        modelMap.addAttribute("page", new PageInfo<>(list));
        modelMap.addAttribute("pd", pd);
        return "ill/illness/illness";
    }

    @RequestMapping("/illness/addpage")
    public String addpage(ModelMap modelMap) {
//        PageData pd = new PageData(request);
        List<Illnessposition> list = illnessTypeService.queryAll();
        modelMap.addAttribute("list", list);
        return "ill/illness/ill_add";
    }

    @RequestMapping("/illness/addsave")
    @ResponseBody
    public ResultJson addsave(Illness illness) {
        illness.setIllId(UUID.randomUUID().toString());
        int num = illnessService.save(illness);
        return ResultJson.out(num);
    }

    @RequestMapping("/illness/editpage")
    public String editpage(String id, ModelMap modelMap) {
        List<Illnessposition> listy = illnessTypeService.queryAll();
        Illness list = illnessService.queryById(id);
        modelMap.addAttribute("list", list);
        modelMap.addAttribute("listy", listy);
        return "ill/illness/ill_edit";
    }

    @RequestMapping("/illness/editsave")
    @ResponseBody
    public ResultJson editsave(Illness illness) {
        int num = illnessService.edit(illness);
        return ResultJson.out(num);
    }

    @RequestMapping("/illness/delete")
    @ResponseBody
    public ResultJson delete(String[] ids) {
//        String names = illnessTypeService.delete(ids);
//        String value = "";
//        ResultJson r=new ResultJson();
//        if (value.equals(names)) {
//            r.setSuccess(true);
//        }else{
//            r.setSuccess(false);
//            r.setMsg(names);
//        }
//        return r;

        int num = illnessService.delete(ids);
        return ResultJson.out(num);
    }
}
